#coding=utf-8
from django.contrib import admin
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect
from DjangoBbs.BbsSetting.models import BbsClassType
from PublicTool import HttpWrite
from PublicTool.BaseAdmin import UserModelAdmin
import models


__author__ = 'czq'
class BbsClassAdmin(UserModelAdmin):
    fields = ('name','parent','typeId','isHidden','adminUsers','visitPerm','isAllowAddTheme','isAllowAddReply','rank',)
    #exclude = ('addUser','site',)
    raw_id_fields = ('visitPerm','adminUsers',)
    list_filter=('isHidden',)
admin.site.register(models.BbsClass,BbsClassAdmin)

import models_vote
#投票选项
class BbsThemeVoteInline(admin.TabularInline):

    #def get_readonly_fields(self, request, obj=None):
    #    return ('longitude','latitude',)

    model = models_vote.BbsThemeVote
    fields = ('content',)
    extra = 5
    ordering = ['rank']
#投票属性
class BbsThemeVoteAttrInline(admin.TabularInline):

    #def get_readonly_fields(self, request, obj=None):
    #    return ('longitude','latitude',)
    #import models_vote
    model = models_vote.BbsThemeVoteAttr
    fields = ('startTime','endTime','totalCount',)
    max_num = 1
    def get_readonly_fields(self, request, obj=None):
        return ('totalCount',)
    #ordering = ['rank']


#旅行属性
class TravelInline(admin.TabularInline):
    from DjangoBbs.Travel.models import RideLine
    model = RideLine
    fields = ('lineType','startTime','endTime',)


class BbsThemeAdmin(UserModelAdmin):
    #发表主题
    def add_view(self, request, form_url='', extra_context=None):
        classId=request.GET.get('classId','')
        self.vote=request.GET.get('vote','False')
        if classId=='':
            return HttpWrite.infoFailure(request,u'无效的板块')
        else:
            try:
                self.classModel=models.BbsClass.objects.get(id=classId)
            except:
                return HttpWrite.infoFailure(request,u"板块不存在")
            else:
                self.classModel=self.classModel
                error=None
                if self.classModel.isHidden==True:
                    error='禁止发贴'
                #elif classId.typeId!=BbsClassType.objects.get(id=1):
                #    error='不是论坛'
                elif self.classModel.visitPerm:
                    #if classModel.visitPerm.allowUsers:
                    #    pass
                    #if classModel.visitPerm.allowRoles:
                    #    pass
                    #if classModel.visitPerm.question:
                    #    pass
                    pass
                if error:
                    return HttpWrite.infoFailure(request,error)
            return super(BbsThemeAdmin,self).add_view(request,form_url,extra_context)
    def render_change_form(self, request, context, add=False, change=False, form_url='', obj=None):
        #classId=request.GET.get('classId','')
        #classModel=models.BbsClass.objects.get(id=classId)
        context.update({
            'classCode':self.classModel.typeId.code,
            #'classModel',self.classModel,
        })
        #raise Exception,self.classModel.typeId.code
        return super(BbsThemeAdmin,self).render_change_form(request,context,add=add,change=change,form_url=form_url,obj=obj)
    #发表后跳转
    def response_add(self, request, obj, post_url_continue='../%s/'):
        #super(BbsThemeAdmin,self).response_add(request,obj,post_url_continue)
        classId=request.GET.get('classId','')
        classModel=models.BbsClass.objects.get(id=classId)
        if classModel.typeId.code=='travel':
            return HttpResponseRedirect('/admin/Travel/rideline/add/?themeId=%s'%(obj.id))
        return HttpResponseRedirect(reverse('bbsTheme_show',args=(obj.id,)))

    def change_view(self, request, object_id, form_url='', extra_context=None):
        classId=request.GET.get('classId','')
        self.classModel=models.BbsClass.objects.get(id=classId)
        self.vote=request.GET.get('vote','False')
        return super(BbsThemeAdmin,self).change_view(request,object_id,form_url,extra_context)
    def response_change(self, request, obj):
        from PublicTool import TimeHelper
        obj.content+=u'(%s)编辑于[%s]'%(request.user.first_name,TimeHelper.get_datetime_str())
        obj.save()
        return HttpResponseRedirect(reverse('bbsTheme_show',args=(obj.id,)))
    def save_model(self, request, obj, form, change):
        if not change:
            obj.classId=self.classModel
            if self.vote=="True":
                obj.isVote=True
            super(BbsThemeAdmin,self).save_model(request,obj,form,change)
            obj.classId.themeCount+=1
            obj.classId.lastTheme=obj
            obj.classId.save()
        else:
            super(BbsThemeAdmin,self).save_model(request,obj,form,change)
    def get_inline_instances(self, request):
        if self.vote=='True':
            self.inlines = [BbsThemeVoteAttrInline,BbsThemeVoteInline,]
            return super(BbsThemeAdmin,self).get_inline_instances(request)
        else:
            self.inlines = []
            return super(BbsThemeAdmin,self).get_inline_instances(request)

    fields = ('themeType','title','content','visitPerm',)
    list_filter = ('isDel','isLock',)

admin.site.register(models.BbsTheme,BbsThemeAdmin)

